package com.lovestudying.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件下载(fileDownload)Controller
 * 
 * @author OldDeer
 *
 */
@WebServlet("/FileDownLoadServlet")
public class FileDownLoadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FileDownLoadServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String op = request.getParameter("op");

		switch (op) {
		case "getAccountFace":
			GetAccountFace(request, response);
			break;
		case "getVideo":
			getVideo(request, response);
			break;
		case "getCourseFace":
			GetCourseFace(request, response);
			break;

		default:
			break;
		}
	}

	private void getVideo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String root = "D:/fileupload/courses";
		String courseId = request.getParameter("courseId");
		// 获取指定的文件名
		String videoURL = request.getParameter("videoURL");

		// 关联文件
		File file = new File(root + "/" + courseId + "/" + videoURL);

		// System.out.println(root + "/" + courseId + "/" + videoURL);
		// 通过字节读取流 读文件
		FileInputStream fis = new FileInputStream(file);

		// 字节数组输出流
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int n;

		// 边读边写
		while ((n = fis.read(b)) != -1) { // 有数据可读
			// 写
			bos.write(b, 0, n);
		}
		fis.close();

		// 通过Servlet输出流将文件以字节数组的形式输出到网页上
		ServletOutputStream sos = response.getOutputStream();
		sos.write(bos.toByteArray());
	}

	/**
	 * 下载头像
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void GetAccountFace(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String root = "D:/fileupload/images/";
		// 获取指定的文件名
		String faceName = request.getParameter("faceName");
		// 关联文件
		File file = new File(root + faceName);

		// 通过字节读取流 读文件
		// System.out.println(root + faceName);
		FileInputStream fis = new FileInputStream(file);

		// 字节数组输出流
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int n;

		// 边读边写
		while ((n = fis.read(b)) != -1) { // 有数据可读
			// 写
			bos.write(b, 0, n);
		}
		fis.close();

		// 通过Servlet输出流将文件以字节数组的形式输出到网页上
		ServletOutputStream sos = response.getOutputStream();
		sos.write(bos.toByteArray());
	}

	/**
	 * 下载头像
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void GetCourseFace(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String root = "D:/fileupload/images/Courses/";
		// 获取指定的文件名
		String faceName = request.getParameter("faceName");
		// 关联文件
		File file = new File(root + faceName);

		// 通过字节读取流 读文件
		// System.out.println(root + faceName);
		FileInputStream fis = new FileInputStream(file);

		// 字节数组输出流
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int n;

		// 边读边写
		while ((n = fis.read(b)) != -1) { // 有数据可读
			// 写
			bos.write(b, 0, n);
		}
		fis.close();

		// 通过Servlet输出流将文件以字节数组的形式输出到网页上
		ServletOutputStream sos = response.getOutputStream();
		sos.write(bos.toByteArray());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
